﻿//#define TRACE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Globalization;
using System.Threading;

namespace AFS.VisitationRequestTool.Addin.Common
{
    public static class Logger
    {
        private static string procName =
                Process.GetCurrentProcess().ProcessName;

        private static TraceSwitch traceSwitch =
                new TraceSwitch("AppTraceLevel", null);

        [ConditionalAttribute("TRACE")]
        public static void Error(string format, params object[] args)
        {
            Error(string.Format(CultureInfo.InvariantCulture, format, args));
        }

        [ConditionalAttribute("TRACE")]
        public static void Error(string message)
        {
            if (traceSwitch.TraceError)
            {
                //StackTrace st = new StackTrace(1);
                //StackFrame sf = st.GetFrame(0);
                //string className = sf.GetMethod().DeclaringType.FullName;
                //string methodName = sf.GetMethod().Name;

                TraceLine(TraceLevel.Info, message);
                   //String.Format("Class:{0}, Method:{1}, Message:{2}", className, methodName, message));
            }
        }

        [ConditionalAttribute("TRACE")]
        public static void Info(string format, params object[] args)
        {
            Info(string.Format(CultureInfo.InvariantCulture, format, args));
        }

        [ConditionalAttribute("TRACE")]
        public static void Info(string message)
        {
            if (traceSwitch.TraceInfo)
            {
                //StackTrace st = new StackTrace(1);
                //StackFrame sf = st.GetFrame(0);
                //string className = sf.GetMethod().DeclaringType.FullName;
                //string methodName = sf.GetMethod().Name;

                TraceLine(TraceLevel.Info, message);
                    //String.Format("Class:{0}, Method:{1}, Message:{2}", className, methodName, message));
            }
        }

        [ConditionalAttribute("TRACE")]
        public static void Verbose(string format, params object[] args)
        {
            Verbose(string.Format(CultureInfo.InvariantCulture, format, args));
        }

        [ConditionalAttribute("TRACE")]
        public static void Verbose(string message)
        {
            if (traceSwitch.TraceVerbose)
            {
                StackTrace st = new StackTrace(1);
                StackFrame sf = st.GetFrame(0);
                string className = sf.GetMethod().DeclaringType.FullName;
                string methodName = sf.GetMethod().Name;

                TraceLine(TraceLevel.Verbose,
                    String.Format("Class:{0}, Method:{1}, Message:{2}", className, methodName, message));
            }
        }

        // TODO: Add Warning/Verbose and other methods in line with Info/Error 

        private static void TraceLine(TraceLevel level, string message)
        {
            string traceLevel = String.Empty;

            switch (level)
            {
                case TraceLevel.Error:
                    traceLevel = "ERROR"; break;

                case TraceLevel.Warning:
                    traceLevel = "WARNING"; break;

                case TraceLevel.Info:
                    traceLevel = "INFO"; break;

                case TraceLevel.Verbose:
                    traceLevel = "VERBOSE"; break;

                default:
                    traceLevel = "DEFAULT"; break;
            }

            string finalMessage = string.Format("{0}, {1}", DateTime.Now, message);
            // CultureInfo.InvariantCulture,
            // "Trace Level:{0}, Process:{1}, Managed Thread Id:{2}, 
            // traceLevel, procName, Thread.CurrentThread.ManagedThreadId, 

            Trace.WriteLine(finalMessage); // , "Addin");
        }

    }
}
